import { CascaderOption } from "element-plus";
import { ResultMessage, BuildTypeListItem } from "../model";
import { axiosPost } from "./http";

export const listTaskType = async () => {
  const rslt = await axiosPost<ResultMessage<BuildTypeListItem[]>>(
    "/listTaskType"
  );
  if (rslt.code !== 200)
    return {
      tree: [],
      data: {},
    };
  const list = rslt.data;

  const tree: CascaderOption[] = [];
  const treeMap: Record<string, CascaderOption[]> = {};
  const typeMap: Record<string, BuildTypeListItem> = {};
  list.map((v) => {
    typeMap[v.type] = v;
    if (!treeMap[v.tag]) {
      tree.push({
        label: v.tag,
        value: v.tag,
        children: [],
      });
      treeMap[v.tag] = [];
    }
    treeMap[v.tag]!.push({
      label: v.name,
      value: v.type,
    });
  });

  for (let item of tree) {
    item.children = treeMap[item.value!];
  }
  return {
    tree,
    data: typeMap,
  };
};
